<?php

namespace App\Http\Controllers\Admin\Goods;

use App\Model\Gshow;
use Illuminate\Http\Request;
use App\Http\Controllers\Common\CommonApi;
use App\Http\Controllers\Controller;

class GoodsShowController extends Controller
{
    public function showedit($id)
    {
        $photodata = Gshow::where('id', $id)->first();
        return view('Admin/Goods/product-photoedit', [
            'photodata' => $photodata, 
            ]); 
    }

    public function showupdate(Request $request)
    {
        foreach($request->files as $pic)
        {
           $pathinfo=$pic;
        }
        $id = $request ->input('id');
        $old_pic = Gshow::select('id','path')->where('id', $id)->first();
        $oldpic =$old_pic->path;
        if(empty($pathinfo)){
            $pic = $oldpic;
        } else {
            $common = new CommonApi;
            $res = $common->uploadToQiNiu($request, 'file_upload');
            if($res){
                    $pic = $res['key'];
            }
            $pic = "http://owv3oi69q.bkt.clouddn.com/".$pic;
        }
        $Gshow =Gshow::where('id', $id)
                     ->update([
                        'gid' => $request ->input('gid'),
                        'state' => $request ->input('state'),
                        'path' => $pic,
                        'descr' => $request ->input('descr'),
                        'created_at' => date('Y-m-d H:i:s', time()),
                        'updated_at' => date('Y-m-d H:i:s', time()),
            ]);
                    
        if($Gshow){
             //修改成功 
            return back()->with('msg', '修改成功');
        }
        
            return back()->with('errorTip', '修改失败');
    }

    public function showdel(Request $request, $id)
    {
        
        $info=Gshow::where('id', $id)->delete();
    
         if ($info) {
                $data = [
                           'status' => 0,
                           'msg'    =>'删除成功',
                           ];
             } else {
                 $data = [
                           'status' => 1,
                           'msg'    =>'删除失败,请稍后再试！',
                           ];
            }
        return $data;
    }
}
